package com.chenglin.mapdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chenglin.mapdemo.bean.LayerBean;
import com.chenglin.mapdemo.bean.ResultBean;
import com.chenglin.mapdemo.view.LayerDialog;
import com.chenglin.mapdemo.view.SearchDialog;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.iv_cancel)
    ImageView ivCancel;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.con_control)
    ConstraintLayout conControl;
    @BindView(R.id.con_measure_distance)
    ConstraintLayout conMeasureDistance;
    @BindView(R.id.con_measure_area)
    ConstraintLayout conMeasureArea;
    @BindView(R.id.iv_layer)
    ImageView ivLayer;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.edit_line)
    View editLine;
    @BindView(R.id.con_search)
    ConstraintLayout conSearch;
    @BindView(R.id.iv_control)
    ImageView ivControl;
    @BindView(R.id.tv_control)
    TextView tvControl;

    @BindView(R.id.iv_measure_area)
    ImageView ivMeasureArea;
    @BindView(R.id.tv_measure_area)
    TextView tvMeasureArea;
    @BindView(R.id.iv_measure_distance)
    ImageView ivMeasureDistance;
    @BindView(R.id.tv_measure_distance)
    TextView tvMeasureDistance;
    @BindView(R.id.iv_discern)
    ImageView ivDiscern;
    @BindView(R.id.tv_discern)
    TextView tvDiscern;

    /**
     * 模拟数据
     */
    private List<String> listTips = new ArrayList<>();
    private List<ResultBean> listResult = new ArrayList<>();
    private List<LayerBean> listLayer = new ArrayList<>();
    private SearchDialog searchDialog;
    private LayerDialog layerDialog;

    private boolean isSelectControl = false;
    private boolean isSelectArea = false;
    private boolean isSelectDistance = false;
    private boolean isSelectWatch = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();

        initView();
    }

    private void initView() {
        //搜索弹窗 初始化
        if (searchDialog == null) {
            searchDialog = new SearchDialog(this, listTips, listResult, new SearchDialog.SearchCallBack() {
                @Override
                public void onTipsCallBack(String tips) {
                    Toast.makeText(MainActivity.this, tips, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onAddressCallBack(ResultBean bean) {
                    Toast.makeText(MainActivity.this, bean.getName(), Toast.LENGTH_SHORT).show();

                }
            });
        }


        /**
         * 地图图层 dialog 初始化
         */
        if (layerDialog == null) {
            layerDialog = new LayerDialog(this, listLayer, new LayerDialog.LayerDialogCallBack() {
                @Override
                public void onClickLisenter(LayerBean.InnerBean bean) {
                    Toast.makeText(MainActivity.this, bean.getInner_name(), Toast.LENGTH_SHORT).show();
                }
            });
        }

    }

    /**
     * 添加模拟数据
     */
    private void initData() {
        //模拟搜索弹窗 数据
        for (int i = 0; i < 7; i++) {
            listTips.add("保健院" + i);
            listResult.add(new ResultBean("山东省青岛市永安医疗站医疗" + i, "山东省青岛市市南区北京路7号" + i));
        }


        // 模拟 多图层弹窗 数据
        List<LayerBean.InnerBean> listInner = new ArrayList<>();
        List<LayerBean.InnerBean> listInner1 = new ArrayList<>();
        List<LayerBean.InnerBean> listInner2 = new ArrayList<>();
        int url = R.drawable.cj;
        listInner.add(new LayerBean.InnerBean("10", R.drawable.pmt_icon, "平面图", true));
        listInner.add(new LayerBean.InnerBean("11", R.drawable.wxt_icon, "卫星图", false));
        listInner1.add(new LayerBean.InnerBean("21", R.drawable.pmt, "平面图", false));
        listInner1.add(new LayerBean.InnerBean("22", R.drawable.wxt, "卫星图", false));
        listInner2.add(new LayerBean.InnerBean("31", R.drawable.tdly, "土地利用", false));
        listInner2.add(new LayerBean.InnerBean("32", R.drawable.gnqh, "功能区划", false));
        listInner2.add(new LayerBean.InnerBean("33", R.drawable.sssw, "水温水深", false));
        listInner2.add(new LayerBean.InnerBean("34", R.drawable.haxgh, "海岸线规划", false));
        listLayer.add(new LayerBean("在线地图", listInner));
        listLayer.add(new LayerBean("离线地图", listInner1));
        listLayer.add(new LayerBean("专题地图", listInner2));


    }

    @OnClick({R.id.iv_cancel, R.id.iv_search, R.id.iv_header, R.id.iv_layer, R.id.con_control, R.id.con_measure_area, R.id.con_measure_distance, R.id.con_discern})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_cancel:  //取消所搜按钮
                break;
            case R.id.iv_search:    //搜索框
                if (!searchDialog.isShowing()) {
                    searchDialog.show();
                }else {
                    searchDialog.dismiss();
                }
                break;
            case R.id.iv_header:  //头像
                break;

            case R.id.iv_layer:  // 图层
                if (!layerDialog.isShowing()) {
                    layerDialog.show();
                }else {
                    layerDialog.dismiss();
                }

                break;
            case R.id.con_control:  //管控
                setSelectStyle(R.drawable.ic_control_select,ivControl,tvControl);

                Toast.makeText(this, "管控", Toast.LENGTH_SHORT).show();
                break;
            case R.id.con_measure_area:  //测量面积
                setSelectStyle(R.drawable.ic_area_select,ivMeasureArea,tvMeasureArea);
                Toast.makeText(this, "测量面积", Toast.LENGTH_SHORT).show();
                break;
            case R.id.con_measure_distance:  //测量距离
                setSelectStyle(R.drawable.ic_distance_select,ivMeasureDistance,tvMeasureDistance);
                Toast.makeText(this, "测量距离", Toast.LENGTH_SHORT).show();
                break;
            case R.id.con_discern:  //识别
                setSelectStyle(R.drawable.ic_watch_select,ivDiscern,tvDiscern);
                Toast.makeText(this, "识别", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 没用 radioGroup  1.目前不清楚业务流程是否符合单选模式 2. 切图不合适 太大
     * 只能用 ImageView +TextView 来实现
     * @param draw
     * @param imageView
     * @param textView
     */
    private void setSelectStyle(int draw, ImageView imageView, TextView textView) {
        // 先将全部按钮 设置为默认模式 不点击
        Glide.with(this).load(R.drawable.ic_control).into(ivControl);
        tvControl.setTextColor(getResources().getColor(R.color.colorTextUnSelect));

        Glide.with(this).load(R.drawable.ic_area).into(ivMeasureArea);
        tvMeasureArea.setTextColor(getResources().getColor(R.color.colorTextUnSelect));

        Glide.with(this).load(R.drawable.ic_distance).into(ivMeasureDistance);
        tvMeasureDistance.setTextColor(getResources().getColor(R.color.colorTextUnSelect));

        Glide.with(this).load(R.drawable.ic_watch).into(ivDiscern);
        tvDiscern.setTextColor(getResources().getColor(R.color.colorTextUnSelect));
        // 将选中按钮调整为选中状态
        Glide.with(this).load(draw).into(imageView);
        textView.setTextColor(getResources().getColor(R.color.colorBlue));
    }


}
